package com.estate.owner.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.Car;
import com.estate.mapper.CarMapper;
import com.estate.owner.service.CarService;
import com.estate.owner.service.OwnerService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Resource
    private OwnerService ownerService;

    @Override
    public JsonResult findById(String id) {
        Car car = this.getById(id);
        return ResultTool.success(car);
    }

    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        List<Car> list = this.list();
        list.forEach(e -> {
            e.setOwner(ownerService.findById(e.getOwnerId()));
        });
        PageInfo<Car> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }

    @Override
    public JsonResult addCar(Car car) {
        boolean b = this.save(car);
        if (!b) {
            return ResultTool.fail(501, "添加失败");
        }
        return ResultTool.success("添加成功");
    }

    @Override
    public JsonResult updateCar(Car car) {
        boolean b = this.updateById(car);
        if (!b) {
            return ResultTool.fail(503, "修改失败");
        }
        return ResultTool.success("修改成功");
    }

    @Override
    public JsonResult deleteCar(String id) {
        boolean b = this.removeById(id);
        if (!b) {
            return ResultTool.fail(502, "删除失败");
        }
        return ResultTool.success("删除成功");
    }
}
